package com.chat.datea.fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.chat.datea.R;
import com.chat.datea.adapter.MusicPlayFragment_GridView_Adapter;
import com.chat.datea.base.BaseFragment;
import com.chat.datea.bean.PlaylistEntity;
import com.chat.datea.utils.fragment.ReplaceFragment;
import com.chat.datea.utils.volley.VolleySingleton;
import com.google.gson.Gson;

/**
 * Created by dllo on 15/10/20.
 */
public class MusicPlaylistFragment extends BaseFragment {
    private GridView gridView;
    private MusicPlayFragment_GridView_Adapter  adapter;
    private VolleySingleton singleton;
    String url = "http://tingapi.ting.baidu.com/v1/restserver/ting?from=android&version=5.6.5.0&method=baidu.ting.diy.gedan&page_size=30&page_no=1";

    @Override
    protected View inflaterView(LayoutInflater inflater, ViewGroup container) {
        view = inflater.inflate(R.layout.fragment_playlist,null);
        return view;
    }

    /**
     * 初始化控件
     * **/
    @Override
    protected void initView() {
        gridView = (GridView) view.findViewById(R.id.gridView);

    }
    /**
     * 初始化数据
     * **/
    @Override
    protected void initData() {
        singleton = VolleySingleton.getVolleySingleton(mActivity);
        getData();
    }
    /**
     * 解析数据
     * **/
    public void getData(){
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                //解析实体类
                final PlaylistEntity entity = gson.fromJson(response, PlaylistEntity.class);
                Log.d("YUEA", "------>" + entity);
                //适配数据
                adapter = new MusicPlayFragment_GridView_Adapter(mActivity, entity);
                gridView.setAdapter(adapter);
                gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    String   list_id =  entity.getContent().get(position).getListid();
                        TodayNewSongFragment newSongFragment = new TodayNewSongFragment(list_id);
                        new ReplaceFragment(newSongFragment, getActivity().getSupportFragmentManager(), R.id.fl_content, true, null);
                    }
                });

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        singleton.addQueue(request, "tag");
    }


}
